#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Apply crystal weighting	                        				#
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 29/07/2016                                             #
# Last Modification: 29/07/2016                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 9
#
#
# MANUAL: This script will introduce neighborhood correlations in the reconstruction by weighting the particles according to how well they agree with the crystal statistics. Ideally, the alignment parameters for particles extracted from the same 2D crystal should be very similar. Even if you don't want to apply this weighting scheme, this script can be used to generate histogram plots of the parameter distributions.
#
# DISPLAY: SPR_DIR
# DISPLAY: SPR_WhichStackAll
# DISPLAY: SPR_REFINE_DIR
# DISPLAY: SPR_CRYSTALREC_DIR
# DISPLAY: SPR_FRLCycle
# DISPLAY: SPR_FRLRef
# DISPLAY: SPR_SIG_EULER
# DISPLAY: SPR_SIG_SHIFT
# DISPLAY: SPR_FRL_parameter_mask
# DISPLAY: SPR_CRYSTALWEIGHTING_PLOT_BINS
# DISPLAY: sample_pixel
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""

set scriptname = ApplyCrystalWeighting
#
set SPR_DIR = ""
set SPR_WhichStackAll = ""
set SPR_REFINE_DIR = ""
set SPR_CRYSTALREC_DIR = ""
set SPR_FRLCycle = ""
set SPR_FRLRef = ""
set SPR_SIG_EULER = ""
set SPR_SIG_SHIFT = ""
set SPR_FRL_parameter_mask = ""
set sample_pixel = ""
set SPR_CRYSTALWEIGHTING_PLOT_BINS = ""

#
set SPR_FREALIGN_DIR = ${SPR_DIR}/FREALIGN/
set SPR_REFINE_DIR = ${SPR_FREALIGN_DIR}/${SPR_REFINE_DIR}/
set SPR_CRYSTALREC_DIR = ${SPR_FREALIGN_DIR}/${SPR_CRYSTALREC_DIR}/
set scriptname = ApplyCrystalWeighting
#$end_vars

if ( -e LOGS/${scriptname}.results ) then
	mv LOGS/${scriptname}.results LOGS/${scriptname}.results.old
endif

# Start script commands:

set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#

# Check if initial files exist:
if ( ! -e ${SPR_CRYSTALREC_DIR} ) then
	mkdir -p ${SPR_CRYSTALREC_DIR}
endif

# ${app_python} ${proc_2dx}/SPR_FrealignCrystalWeighting.py ${SPR_REFINE_DIR}/${SPR_STACK_ROOTNAME}_${SPR_WhichStackAll}_${SPR_FRLCycle}_r${SPR_FRLRef}.par ${SPR_CRYSTALREC_DIR}/${SPR_STACK_ROOTNAME}_${SPR_WhichStackAll}-xweighted_${SPR_FRLCycle}_r1.par ${SPR_SIG_EULER} ${SPR_SIG_SHIFT} ${SPR_FRL_parameter_mask} ${sample_pixel}
${app_python} ${proc_2dx}/SPR_FrealignCrystalWeighting.py --sigma_euler ${SPR_SIG_EULER} --sigma_shift ${SPR_SIG_SHIFT} --pmask ${SPR_FRL_parameter_mask} --angpix ${sample_pixel} ${SPR_REFINE_DIR}/${SPR_STACK_ROOTNAME}_${SPR_WhichStackAll}_${SPR_FRLCycle}_r${SPR_FRLRef}.par

mv ${SPR_REFINE_DIR}/${SPR_STACK_ROOTNAME}_${SPR_WhichStackAll}_${SPR_FRLCycle}_r${SPR_FRLRef}-xweighted.par ${SPR_CRYSTALREC_DIR}/${SPR_STACK_ROOTNAME}_${SPR_WhichStackAll}-xweighted_${SPR_FRLCycle}_r1.par

echo ":: "
echo ":: Done!"
echo ":: "
